package com.github.beihaifeiwu.myspring.http.feign;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Logs to SLF4J at the debug level, if the underlying logger has debug logging enabled.  The
 * underlying logger can be specified at construction-time, defaulting to the logger for {@link
 * feign.Logger}.
 */
public class FeignSlf4jLogger extends feign.Logger {

    private final Logger logger;
    private final org.slf4j.event.Level level;

    public FeignSlf4jLogger(String name, org.slf4j.event.Level level) {
        this.logger = LoggerFactory.getLogger(name);
        this.level = level;
    }

    @Override
    protected void log(String configKey, String format, Object... args) {
        switch (level) {
            case ERROR:
                logger.error(String.format(methodTag(configKey) + format, args));
                break;
            case WARN:
                logger.warn(String.format(methodTag(configKey) + format, args));
                break;
            case INFO:
                logger.info(String.format(methodTag(configKey) + format, args));
                break;
            case DEBUG:
                logger.debug(String.format(methodTag(configKey) + format, args));
                break;
            case TRACE:
                logger.trace(String.format(methodTag(configKey) + format, args));
                break;
            default:
                break;
        }
    }

}